package Application.MapApplication.view.ui
{	
	import MapBase.logic.MapBase;
	import MapBase.logic.vo.MapBaseVO;
	
	import Application.MapApplication.def.Def;
	import def.XG;
	
	import fl.controls.Button;
	import fl.controls.CheckBox;
	import fl.controls.DataGrid;
	import fl.controls.Slider;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.events.SliderEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MainTopMed extends UIBaseMed
	{
		public var view : Sprite;
		
		private var _vo : MapBaseVO = MapBase.vo;
		
		private var _createNewBtn : Button;
		private var _elementBtn : Button;
		private var _clearBtn : Button;
		private var _btnSave : Button;
		private var _btnOpen : Button;
		
		private var _ckbObjLayer : CheckBox;
		private var _ckbGridLayer : CheckBox;
		private var _ckbBkLayer : CheckBox;
		private var _ckbPutDot : CheckBox;
		private var _ckbDelObj : CheckBox;
		private var _ckbShowGrid : CheckBox;
		private var _ckbDragEntity : CheckBox;
		private var _ckbEIsoObj : CheckBox;
		private var _ckbIsoFloor : CheckBox;
		
		private var _sldGridAlpha : Slider;
		private var _sldWorldIsoAlpha : Slider;
		
		public function MainTopMed()
		{	
			super(Def.MainTop,[Def.STAGE_ALIGN_LEFT,Def.STAGE_ALIGN_TOP]);
			
			view = this.viewComponent as Sprite;
		}
		
		override public function onRegister():void
		{
			super.onRegister();
			
			trace("Register MainTop");
			
			_createNewBtn = view["Btn_Create"];
			_elementBtn = view["Btn_Element"];
			_clearBtn = view["Btn_ClearPan"];
			_btnSave = view["Btn_Save"];
			_btnOpen = view["Btn_Open"];
			
			_ckbObjLayer = view["Ckb_ObjLayer"];
			_ckbGridLayer = view["Ckb_GridLayer"];
			_ckbBkLayer = view["Ckb_BkLayer"];
			_ckbPutDot = view["Ckb_PutDot"];
			_ckbDelObj = view["Ckb_DelObj"];
			_ckbShowGrid = view["Ckb_ShowGrid"];
			_ckbDragEntity = view["Ckb_DragEntity"];
			_ckbEIsoObj = view["Ckb_EIsoObj"];
			_ckbIsoFloor = view["Ckb_IsoObjFloor"];
			
			_sldGridAlpha = view["Sld_GridAlpha"];
			_sldWorldIsoAlpha = view["Sld_WorldIsoLayerAlpha"];
			
			setData(_vo);
			
			_createNewBtn.addEventListener(MouseEvent.CLICK,OnClickCreateNewBtn);
			_elementBtn.addEventListener(MouseEvent.CLICK,OnClickElementBtn);
			_clearBtn.addEventListener(MouseEvent.CLICK,OnClearPan);
			_btnSave.addEventListener(MouseEvent.CLICK,OnSaveToXml);
			_btnOpen.addEventListener(MouseEvent.CLICK,OnOpenXml);
			
			_ckbObjLayer.addEventListener(Event.CHANGE,OnSelectObjLayer);
			_ckbGridLayer.addEventListener(Event.CHANGE,OnSelectGridLayer);
			_ckbBkLayer.addEventListener(Event.CHANGE,OnSelectBkLayer);
			_ckbPutDot.addEventListener(Event.CHANGE,OnSelectPutDot);
			_ckbDelObj.addEventListener(Event.CHANGE,OnDelObj);
			_ckbShowGrid.addEventListener(Event.CHANGE,OnShowGrid);
			_ckbDragEntity.addEventListener(Event.CHANGE,OnDragEntity);
			_ckbEIsoObj.addEventListener(Event.CHANGE,OnEIsoObj);
			_ckbIsoFloor.addEventListener(Event.CHANGE,OnIsoObjFloor);
			
			_sldGridAlpha.addEventListener(Event.CHANGE,OnGridAlphaChanged);
			_sldWorldIsoAlpha.addEventListener(Event.CHANGE,OnGridWorldLayerChanged);
		
		}
		
		private function setData(vo:MapBaseVO):void
		{
			_ckbObjLayer.selected = vo.sleObjLayer;
			_ckbGridLayer.selected = vo.sleGridLayer;
//			_ckbBkLayer = vo.b;
			_ckbPutDot.selected = vo.slePutDot;
			_ckbDelObj.selected = vo.sleDelObj;
			_ckbShowGrid.selected = vo.sleShowGrid;
			_ckbDragEntity.selected = vo.sleDragEntity;
//			_ckbEIsoObj = vo.sle;
//			_ckbIsoFloor = vo.;
		}
		
		override public function listNotificationInterests() : Array
		{
			var notificatons : Array = super.listNotificationInterests();
			
			notificatons=notificatons.concat([Def.MainTop_Show,Def.MainTop_Close,Def.MainTop_Turn,Def.STAGE_SIZE_CHANGED]);
			
			return notificatons;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case Def.MainTop_Show:
					XG.openUI(this.mediatorName);
					break;
				case Def.MainTop_Close:
					destory();
					XG.closeUI(this.mediatorName);
					break;
				case Def.MainTop_Turn : 
//					GameU.turnUI(Define,viewComponent as Sprite);
					break;
				default :
					break;
			}
			
			super.handleNotification(notification);
		}
		
		override public function onRemove():void
		{	
			super.onRemove();
			
			trace("Remove MainTop");
		}
		
		private function destory() : void
		{
			facade.removeMediator(Def.MainTop);
			facade.removeProxy(Def.MainTop);
			
			_createNewBtn.removeEventListener(MouseEvent.CLICK,OnClickCreateNewBtn);
			_elementBtn.removeEventListener(MouseEvent.CLICK,OnClickElementBtn);
			_clearBtn.removeEventListener(MouseEvent.CLICK,OnClearPan);
			_btnSave.removeEventListener(MouseEvent.CLICK,OnSaveToXml);
			_btnOpen.removeEventListener(MouseEvent.CLICK,OnOpenXml);
			
			_ckbObjLayer.removeEventListener(Event.CHANGE,OnSelectObjLayer);
			_ckbGridLayer.removeEventListener(Event.CHANGE,OnSelectGridLayer);
			_ckbBkLayer.removeEventListener(Event.CHANGE,OnSelectBkLayer);
			_ckbPutDot.removeEventListener(Event.CHANGE,OnSelectPutDot);
			_ckbDelObj.removeEventListener(Event.CHANGE,OnDelObj);
			_ckbShowGrid.removeEventListener(Event.CHANGE,OnShowGrid);
			_ckbDragEntity.removeEventListener(Event.CHANGE,OnDragEntity);
			_ckbEIsoObj.removeEventListener(Event.CHANGE,OnEIsoObj);
			_ckbIsoFloor.removeEventListener(Event.CHANGE,OnIsoObjFloor);
			
			_sldGridAlpha.removeEventListener(Event.CHANGE,OnGridAlphaChanged);
			_sldWorldIsoAlpha.removeEventListener(Event.CHANGE,OnGridWorldLayerChanged);
			
			viewComponent = null;
		}
		
		private function OnIsoObjFloor(evt : Event):void
		{
			sendNotification(Def.MapEdit_IsoObjFloor,_ckbIsoFloor.selected);
			
		}
		
		private function OnEIsoObj(evt : Event):void
		{
			sendNotification(Def.MapEdit_EIsoObj,_ckbEIsoObj.selected);
			
		}
		
		/**
		 * save 
		 */		
		private function OnSaveToXml(evt : MouseEvent):void
		{
			sendNotification(Def.MapEdit_Save);
		}
		
		private function OnOpenXml(evt : MouseEvent) : void
		{
			sendNotification(Def.MapEdit_OpenMapXML);
		}
		
		private function OnClickCreateNewBtn(evt : MouseEvent) : void
		{
			if(!facade.hasMediator(Def.CreateMap))
			{
				XG.openUI(Def.CreateMap,CreateMapMed);
			}
		}
		
		private function OnClickElementBtn(evt : MouseEvent) : void
		{
			if(!facade.hasMediator(Def.MapEditMapList))
			{
				XG.openUI(Def.MapEditMapList,MapEditMapListMed);
			}
		}
		
		private function OnClearPan(evt : MouseEvent) : void
		{
			sendNotification(Def.MapEdit_ClearPan);
		}
		
		private function OnSelectObjLayer(evt : Event) : void
		{
			sendNotification(Def.MapEdit_SelectObjLayer,_ckbObjLayer.selected);
		}
		
		private function OnSelectGridLayer(evt : Event) : void
		{
			sendNotification(Def.MapEdit_SelectGridLayer,_ckbGridLayer.selected);
		}
		
		private function OnSelectBkLayer(evt : Event) : void
		{
			sendNotification(Def.MapEdit_SelectBkLayer,_ckbBkLayer.selected);
		}
		
		private function OnSelectPutDot(evt : Event) : void
		{
			sendNotification(Def.MapEdit_PutDot,_ckbPutDot.selected);
		}
		
		private function OnDelObj(evt : Event) : void
		{
			sendNotification(Def.MapEdit_DelObj,_ckbDelObj.selected);
		}
		
		private function OnShowGrid(evt : Event) : void
		{
			sendNotification(Def.MapEdit_ShowGrid,_ckbShowGrid.selected);
		}
		
		private function OnDragEntity(evt : Event) : void
		{
			sendNotification(Def.MapEdit_DragEntity,_ckbDragEntity.selected);
		}
		
		protected function OnGridAlphaChanged(event:SliderEvent):void
		{
			sendNotification(Def.MapEdit_GridAlphaChanged,_sldGridAlpha.value);
		}
		
		private function OnGridWorldLayerChanged(evt : SliderEvent):void
		{
			sendNotification(Def.MapEdit_WorldLayerAlphaChanged,_sldWorldIsoAlpha.value);
		}
	}
}